// import 'package:flutter/services.dart';
// import 'package:url_launcher/url_launcher.dart';
// import 'categroy_entry.dart';

import 'package:blocks_blkk/page/categroy_entry.dart';
import 'package:url_launcher/url_launcher.dart';

import '../app_configure.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'app_intl.dart';
import 'mixins/background_mixin.dart';

/*
 * Main APP 首页入口 
 */
class AppEntry extends StatefulWidget {
  const AppEntry({super.key});

  @override
  AppEntryState createState() => AppEntryState();
}

class AppEntryState extends State<AppEntry> with BackgroundMixin {
  bool _showOption = false;
  String _languangImage = AppIntl.getString('languangImage');
  List get _itemName => [
        {
          'name': AppIntl.getString('entry_setup'),
          'image': 'appentry_setup.png',
          'index': 0,
        },
        {
          'name': AppIntl.getString('control_control'),
          'image': 'appentry_control.png',
          'index': 1,
        }
      ];

  @override
  void initState() {
    super.initState();
    //_requestPermission();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      decoration: const BoxDecoration(
        image: DecorationImage(
          image: AssetImage('assets/background.png'),
          fit: BoxFit.fill,
        ),
      ),

      //功能入口
      child: Stack(
        children: [
          logo(),
          _function(),
          _languageBtn(),
          _languageOption(),
        ],
      ),
    );
  }

  //语言按钮
  Widget _languageBtn() {
    return Positioned(
      right: ScreenUtil().setWidth(20.0),
      top: ScreenUtil().setWidth(30.0),
      width: ScreenUtil().setWidth(90.0),
      height: ScreenUtil().setWidth(60.0),
      child: GestureDetector(
        onTap: () {
          setState(() {
            _showOption = true;
          });
        },
        child: Container(
          decoration: BoxDecoration(
            image: DecorationImage(
              image: AssetImage(_languangImage),
            ),
          ),
        ),
      ),
    );
  }

  //语言选项
  Widget _languageOption() {
    if (!_showOption) {
      return const SizedBox(width: 0, height: 0);
    }
    return Positioned(
      right: ScreenUtil().setWidth(20.0),
      top: ScreenUtil().setWidth(90.0),
      width: ScreenUtil().setWidth(90.0),
      height: ScreenUtil().setWidth(120.0),
      child: Container(
        color: Colors.green,
        child: Column(
          children: [
            _languageOptionItem('assets/state_ch.png', 'cn'),
            _languageOptionItem('assets/state_en.png', 'en')
          ],
        ),
      ),
    );
  }

  //语言选项
  Widget _languageOptionItem(String image, String lang) {
    return GestureDetector(
      onTap: () {
        setState(() {
          _showOption = false;
          _languangImage = image;
          AppConfig.instance.language = lang;
          AppConfig.instance.saveToFile();
        });
      },
      child: Container(
        width: ScreenUtil().setWidth(90.0),
        height: ScreenUtil().setWidth(60.0),
        decoration: BoxDecoration(
          image: DecorationImage(
            image: AssetImage(image),
          ),
        ),
      ),
    );
  }

  Widget _function() {
    return Positioned(
      left: ScreenUtil().setWidth(80.0),
      right: ScreenUtil().setWidth(80.0),
      height: ScreenUtil().setHeight(400.0),
      top: ScreenUtil().setHeight(180.0),
      child: Container(
        child: _functionItems(),
      ),
    );
  }

  //列表
  Widget _functionItems() {
    if (_itemName.length < 4) {
      return Row(
          mainAxisAlignment: MainAxisAlignment.spaceAround,
          children:
              _itemName.map((e) => _itemBuilder(context, e['index'])).toList());
    } else {
      return ListView.builder(
        scrollDirection: Axis.horizontal,
        itemExtent: ScreenUtil().setHeight(400),
        shrinkWrap: true,
        physics: const BouncingScrollPhysics(),
        itemCount: _itemName.length,
        itemBuilder: _itemBuilder,
      );
    }
  }

  //每一项事件
  GestureTapCallback onTap(int index) {
    final Uri url = Uri.parse('https://bulukebooklet.wanjushuo.wang/');

    return () async {
      // 打开链接
      if (index == 0) {
        launchUrl(url);
      } else {
        // 跳转页面
        Navigator.of(context).push(
          MaterialPageRoute(
            builder: (context) {
              return CategroyEntry();
            },
          ),
        );
      }
    };
  }

  Widget _itemBuilder(context, index) {
    return GestureDetector(
      onTap: onTap(index),
      child: Container(
        alignment: const Alignment(0, 1),
        padding: EdgeInsets.only(bottom: ScreenUtil().setHeight(10)),
        width: ScreenUtil().setHeight(300),
        height: ScreenUtil().setHeight(439),
        decoration: BoxDecoration(
          image: DecorationImage(
              image: AssetImage('assets/${_itemName[index]['image']}'),
              fit: BoxFit.fill),
        ),
        child: Text(
          _itemName[index]['name'],
          style: TextStyle(
              color: Colors.black,
              decoration: TextDecoration.none,
              fontSize: ScreenUtil().setSp(32)),
        ),
      ),
    );
  }
}
